package com.sinochem.yunlian.ship.base.rest.app;

import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.base.biz.BsMaterialCategoryBiz;
import com.sinochem.yunlian.ship.base.biz.BsPortBiz;
import com.sinochem.yunlian.ship.base.entity.BsMaterialCategory;
import com.sinochem.yunlian.ship.base.entity.BsPort;
import com.sinochem.yunlian.ship.base.vo.response.MaterialCategoryVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author panyanan
 * @date 2017-11-20 21:02
 *
 * */
@IgnoreToken
@RestController
@RequestMapping("/m/materialCategory")
public class MBsMaterialCategoryController extends BaseController<BsPortBiz,BsPort> {


    @Autowired
    private BsMaterialCategoryBiz bsMaterialCategoryBiz;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查找港口
     * */
    @RequestMapping(value = "/findAll/anon", method = RequestMethod.POST)
    public RestResponse findPorts(){
        RestResponse result = new RestResponse();
        List<MaterialCategoryVo> listVo = new ArrayList<>();

        //查询所有已经启用的货品种类信息 目前只有两级后期改为递归
        if (redisTemplate.hasKey(RedisKey.KEY_MATERIAL_CATEGORY_ALL)) {
            String allCategory = (String) redisTemplate.opsForValue().get(RedisKey.KEY_MATERIAL_CATEGORY_ALL);
            listVo = JSONObject.parseArray(allCategory,MaterialCategoryVo.class);
        } else {
            List<BsMaterialCategory> list = bsMaterialCategoryBiz.findAllMaterialCategory();
            for (BsMaterialCategory category : list) {
                if (category.getLevel() == 1) {
                    MaterialCategoryVo vo = transformEntity2Vo(category);
                    for (BsMaterialCategory categoryTwo : list) {
                        if (categoryTwo.getLevel() == 2 && categoryTwo.getParentId().equals(vo.getId())) {
                            if (vo.getChildrens() == null) {
                                vo.setChildrens(new ArrayList<>());
                            }
                            MaterialCategoryVo voTwo = transformEntity2Vo(categoryTwo);
                            for (BsMaterialCategory categoryThree : list) {
                                if (categoryThree.getLevel() == 3 && categoryThree.getParentId().equals(categoryTwo.getId())) {
                                    if (voTwo.getChildrens() == null) {
                                        voTwo.setChildrens(new ArrayList<>());
                                    }
                                    MaterialCategoryVo voThree = transformEntity2Vo(categoryThree);
                                    voTwo.getChildrens().add(voThree);
                                }
                            }
                            if (voTwo.getChildrens() != null && voTwo.getChildrens().size() != 0) {
                                vo.getChildrens().add(voTwo);
                            }
                        }
                    }
                    if (vo.getChildrens() != null && vo.getChildrens().size() != 0) {
                        listVo.add(vo);
                    }
                }

            }
            redisTemplate.opsForValue().set(RedisKey.KEY_MATERIAL_CATEGORY_ALL,JSONObject.toJSONString(listVo));
        }
        result.setData(listVo);
        return result;
    }

    @RequestMapping(value = "/findByLevel/anon", method = RequestMethod.POST)
    public RestResponse findByLevel(){
        RestResponse result = new RestResponse();
        List<MaterialCategoryVo> listVo = new ArrayList<>();

        //查询所有已经启用的货品种类信息 目前只有两级后期改为递归
        if (redisTemplate.hasKey(RedisKey.KEY_MATERIAL_CATEGORY_LEVEL)) {
            String allCategory = (String) redisTemplate.opsForValue().get(RedisKey.KEY_MATERIAL_CATEGORY_LEVEL);
            listVo = JSONObject.parseArray(allCategory,MaterialCategoryVo.class);
        } else {
            List<BsMaterialCategory> list = bsMaterialCategoryBiz.findByLevel();
            for (BsMaterialCategory category : list) {
                if (category.getLevel() == 2) {
                    MaterialCategoryVo vo = transformEntity2Vo(category);
                    listVo.add(vo);
                    for (BsMaterialCategory categoryOne : list) {
                        if (categoryOne.getLevel() == 3 && categoryOne.getParentId().equals(vo.getId())) {
                            if (vo.getChildrens() == null) {
                                vo.setChildrens(new ArrayList<>());
                            }
                            MaterialCategoryVo voOne = transformEntity2Vo(categoryOne);
                            vo.getChildrens().add(voOne);
                        }
                    }
                }

            }
            redisTemplate.opsForValue().set(RedisKey.KEY_MATERIAL_CATEGORY_LEVEL,JSONObject.toJSONString(listVo));
        }
        result.setData(listVo);
        return result;
    }


    public MaterialCategoryVo transformEntity2Vo (BsMaterialCategory bsMaterialCategory) {
        MaterialCategoryVo vo =  new MaterialCategoryVo();
        vo.setId(bsMaterialCategory.getId());
        vo.setCategoryName(bsMaterialCategory.getCategoryName());
        vo.setEnName(bsMaterialCategory.getEnName());
        vo.setLevel(bsMaterialCategory.getLevel());
        vo.setParentId(bsMaterialCategory.getParentId());
        vo.setSeqNum(bsMaterialCategory.getSeqNum());
        return vo;
    }



}